import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class DialogUtils {
  DialogUtils._private();

  /// 底部的选择弹框
  static void showItemSelectDialog(
    BuildContext context, {
    required List<String> data,
    required ValueChanged<int> onConfirm,
    int initialItem = 0,
    double height = 343,
    String title = '',
    String cancelText = '取消',
    String confirmText = '确定',
  }) {
    showModalBottomSheet(
        backgroundColor: Colors.transparent,
        context: context,
        builder: (BuildContext context) {
          int position = 0;
          return Container(
            padding: const EdgeInsets.all(16),
            height: height,
            decoration: const BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(16),
                    topRight: Radius.circular(16))),
            child: Column(
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    TextButton(
                      onPressed: () {
                        Navigator.pop(context);
                      },
                      child: Text(
                        cancelText,
                        style: const TextStyle(
                            fontSize: 16, color: Colors.black87),
                      ),
                    ),
                    Text(
                      title,
                      style: const TextStyle(
                          fontSize: 16,
                          color: Colors.black,
                          fontWeight: FontWeight.w600),
                    ),
                    TextButton(
                      onPressed: () {
                        Navigator.pop(context);
                        if (position >= 0 && position < data.length) {
                          onConfirm(position);
                        }
                      },
                      child: Text(confirmText,
                          style: const TextStyle(
                              fontSize: 16, color: Colors.blue)),
                    )
                  ],
                ),
                Expanded(
                  child: CupertinoPicker.builder(
                    scrollController:
                        FixedExtentScrollController(initialItem: initialItem),
                    offAxisFraction: 0,
                    itemExtent: 50,
                    onSelectedItemChanged: (index) {
                      position = index;
                    },
                    itemBuilder: (BuildContext context, int index) {
                      return Container(
                        padding: const EdgeInsets.symmetric(horizontal: 10),
                        alignment: Alignment.center,
                        child: Text(data[index],
                            style: const TextStyle(
                                color: Colors.black, fontSize: 16),
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis),
                      );
                    },
                    childCount: data.length,
                  ),
                ),
              ],
            ),
          );
        });
  }
}
